<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/22
 * Time: 14:26
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Service\Admin\CategoryService;
use App\Constant\ErrorConstant;
use Illuminate\Support\Facades\Validator;
use App\Http\Model\Mysql\CategoryModel;

class CategoryController extends BaseController
{
    protected $service;

    public function __construct(Request $request, CategoryService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 分类列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listCat()
    {
        [$flag, $result]    =   $this->service->catList();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_CAT_LIST_ERROR, $result);
        }
    }

    /**
     * 分类删除
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteCat()
    {
        $validator  =   Validator::make($this->request->post(), [
            'cat_id'    =>  'required|integer|exists:mini_category,id'
        ], [
            'cat_id.required'   =>  '分类ID不能为空',
            'cat_id.integer'    =>  '分类ID必须为整数',
            'cat_id.exists'     =>  '分类不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_CAT_DELETE_ERROR, $validator->errors()->first());
        }

        $cat_id =   (int) $this->request->post('cat_id');
        [$flag, $result]    =   $this->service->catDelete($cat_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_CAT_DELETE_ERROR, $result);
        }
    }

    /**
     * 分类下架
     * @return \Illuminate\Http\JsonResponse
     */
    public function offCat()
    {
        $validator  =   Validator::make($this->request->post(), [
            'cat_id'    =>  'required|integer|exists:mini_category,id',
            'cat_status'=>  'required|integer|between:0,1'
        ], [
            'cat_id.required'   =>  '分类ID不能为空',
            'cat_id.integer'    =>  '分类ID必须为整数',
            'cat_id.exists'     =>  '分类不存在',

            'cat_status.required'=>  '分类操作不能为空',
            'cat_status.integer'=>  '分类操作必须为整数',
            'cat_status.between'=>  '分类操作选择错误',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_CAT_OFF_ERROR, $validator->errors()->first());
        }

        $cat_id     =   (int) $this->request->post('cat_id');
        $cat_status =   (int) $this->request->post('cat_status');
        [$flag, $result]    =   $this->service->catOff($cat_id, $cat_status);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_CAT_OFF_ERROR, $result);
        }
    }

    /**
     * 分类添加
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCat()
    {
        $validator  =   Validator::make($this->request->all(), [
            'cat_name'  =>  'required|string|max:50',
            'parent_id' =>  ['required', 'integer', function ($attribute, $value, $fails) {
                if ($value != 0) {
                    if (!CategoryModel::where(['id' => $value])->exists()) {
                        $fails('分类上级不存在');
                    }
                }
            }],
            'sorting'   =>  'required|integer'
        ], [
            'cat_name.required' =>  '分类名称不能为空',
            'parent_id.required'=>  '分类上级不能为空',
            'sorting.required'  =>  '分类排序不能为空',

            'parent_id.integer' =>  '分类上级必须为整数',
            'sorting.integer'   =>  '分类排序必须为整数',
            'cat_name.max'      =>  '分类名称过长'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_CAT_ADD_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->catAdd($this->request->all());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_CAT_ADD_ERROR, $result);
        }
    }

}
